package assignment1;

import Variables.Variables;

public class Heuristic extends Variables {

	private int iteration=0;
	private int wavelength=0;
	boolean empty(){
		for (Demand m: demands){

			if (m.getTrafficDemand() != 0)
				return false;
		}
		return true ;
	}

	public  void calculate (){
		// Verify whether all traffic demands are satisfied 
		while (!empty())
		{	
			for (int m=0 ; m< demands.length; m++){
				Demand d= demands[m];
				int trafficDemand= d.getTrafficDemand();
				if( trafficDemand!=0){

					// for every specified route of a pair node m
					for (int r =0; r< (d.getnumberRoutes()-1) ; r++)
					{ 
						Z zell = traffic [r][m];
						int [] route = zell.getRoute();

						// look for the Link that has the biggest Wavelength number
						for (int l:route){
							//get the load of a specified link within the road
							int maxLoad =links[l-1].getNumberWavelength();
							if (zell.getMaxLoad() < maxLoad ){
								zell.setMaxLoad(maxLoad);
							}//if
						}// for links l in route r, at this point we have the max load and its links index

						//take the min 
					}//for all routes r
					// find the min route to be established for one connection
					for (int r =0; r<(d.getnumberRoutes()-1); r++)
					{ 
						Z zell = traffic [r][m];
						int minMaxLoad = d.getminMaxLoad();
						int maxLoad = zell.getMaxLoad();
						// for the first test it is always true
						if ( maxLoad < minMaxLoad  )
							d.setminMaxLoad(maxLoad);
						d.setindexMinMaxLoad(r);
					}// for all routes

					//for this demand which is the pair node, the nominated route is selected
					d.setTrafficDemand(trafficDemand -1);
					// add a new wavelength to all links in the nominated link
					Z chosenZell = traffic [d.getindexMinMaxLoad()][m];
					int[] route = chosenZell.getRoute();
					for (int l: route){
						//Increment the load == wavelength in each link of the chosen route
						links[l-1].setNumberWavelength();
					}
					iteration++;
					//show the new link table 
					System.out.println("-------------------------------------------------------------");
					System.out.println("|   L1   |   L2   |   L3   |   L4   |   L5   |   L6   |   L7  |");
					System.out.println("-------------------------------------------------------------");
					System.out.println("|   "+links[0].getNumberWavelength()+"  |   "+links[1].getNumberWavelength()+"  |   "+links[2].getNumberWavelength()+"  |   "+links[3].getNumberWavelength()+"  |   "+links[4].getNumberWavelength()+"  |   "+links[5].getNumberWavelength()+"  |   "+links[6].getNumberWavelength()+"|");
					System.out.println("-------------------------------------------------------------");
				}// if demands are not all satisfied
			}//for m
		}//while not empty
		// get the number of wavelength associated 
		for (Link l: links){
			if (l.getNumberWavelength() > wavelength)
				wavelength=l.getNumberWavelength();
		}// for all links
		System.out.println("final report: global routes checking Algorithm");
		System.out.println(">>>>number of iterations :"+ iteration);
		System.out.println(">>>>number of wavelength allocated :"+wavelength);
	};

	public void simpleheuristic(){

		// verify whether all traffic demands are satisfied 
		while (!empty())
		{	
			for (int m=0 ; m< demands.length; m++){
				Demand d= demands[m];
				int trafficDemand= d.getTrafficDemand();
				if( trafficDemand!=0){

					// take only the first existing route
					Z zell = traffic [0][m];
					int [] route = zell.getRoute();

					//for this demand which is the pair node, the nominated route is selected
					d.setTrafficDemand(trafficDemand -1);
					// add a new wavelength to all links in the nominated link
					for (int l: route){
						//Increment the load == wavelength in each link of the chosen route
						links[l-1].setNumberWavelength();
					}				
					// set the new cell in the table
					traffic[0][m] = zell;
					iteration++;
					//show the new link table 
					System.out.println("-------------------------------------------------------------");
					System.out.println("|   L1   |   L2   |   L3   |   L4   |   L5   |   L6   |   L7  |");
					System.out.println("-------------------------------------------------------------");
					System.out.println("|   "+links[0].getNumberWavelength()+"  |   "+links[1].getNumberWavelength()+"  |   "+links[2].getNumberWavelength()+"  |   "+links[3].getNumberWavelength()+"  |   "+links[4].getNumberWavelength()+"  |   "+links[5].getNumberWavelength()+"  |   "+links[6].getNumberWavelength()+"|");
					System.out.println("-------------------------------------------------------------");

				}// if demands are not all satisfied
			}//for m
		}//while not empty
		// get the number of wavelength associated 
		for (Link l: links){
			if (l.getNumberWavelength() > wavelength)
				wavelength=l.getNumberWavelength();
		}// for all links
		System.out.println("final report: shortest path algorithm");
		System.out.println(">>>>number of iterations :"+ iteration);
		System.out.println(">>>>number of wavelength allocated :"+wavelength);
	}
}
